package status

import (
	"strconv"

	"git.cloud.inspur.com/ichain/ichain-protos-go/common"
	pb "git.cloud.inspur.com/ichain/ichain-protos-go/inode"
	grpcCodes "google.golang.org/grpc/codes"
)

// Code 返回码
type Code uint32

const (
	OK Code = 0

	Unknown Code = 1

	ConnectionFailed Code = 2

	EndorsementMismatch Code = 3

	EmptyCert Code = 4

	Timeout Code = 5

	NoInodesFound Code = 6

	MultipleErrors Code = 7

	SignatureVerificationFailed Code = 8

	MissingEndorsement Code = 9

	QueryEndorsers Code = 11

	GenericTransient Code = 12

	ContractNameNotFound Code = 23

	PvtDataDisseminationFailed Code = 24
)

var CodeName = map[int32]string{
	0:  "OK",
	1:  "UNKNOWN",
	2:  "CONNECTION_FAILED",
	3:  "ENDORSEMENT_MISMATCH",
	4:  "EMPTY_CERT",
	5:  "TIMEOUT",
	6:  "NO_PEERS_FOUND",
	7:  "MULTIPLE_ERRORS",
	8:  "SIGNATURE_VERIFICATION_FAILED",
	9:  "MISSING_ENDORSEMENT",
	11: "QUERY_ENDORSERS",
	12: "GENERIC_TRANSIENT",
	23: "CONTRACT_NAME_NOT_FOUND",
	24: "PRIVATE_DATA_DISSEMINATION_FAILED",
}

func (c Code) ToInt32() int32 {
	return int32(c)
}

func (c Code) String() string {
	if s, ok := CodeName[c.ToInt32()]; ok {
		return s
	}
	return strconv.Itoa(int(c))
}

func ToSDKStatusCode(c int32) Code {
	return Code(c)
}

func ToGRPCStatusCode(c int32) grpcCodes.Code {
	return grpcCodes.Code(c)
}

func ToInodeStatusCode(c int32) common.Status {
	return ToCommonStatusCode(c)
}

func ToConsenserStatusCode(c int32) common.Status {
	return ToCommonStatusCode(c)
}

func ToCommonStatusCode(c int32) common.Status {
	return common.Status(c)
}

func ToTransactionValidationCode(c int32) pb.TxValidationCode {
	return pb.TxValidationCode(c)
}
